package com.smartmedical.system.patient.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Value;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
@Component
public class EmailUtil {


    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String fromEmail;

    /**
     * 存入缓存
     * @param code
     * @param email
     * @return
     */
    @CachePut(value = "bbb",key = "#email")
    public String saveCode(String code,String email){
        return code;
    }

    /**
     * 从缓存获取
     * @param email
     * @return
     */
    @Cacheable(value = "bbb",key = "#email")
    public String getCode(String email){
        return null;
    }


    //邮件发送 ===》封装的验证码发送方法
    public void sendCode(String code,String email){

        MimeMessage mimeMessage = javaMailSender.createMimeMessage();

        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setSubject("验证码通知");
            helper.setFrom(fromEmail);
            //helper.setText("<a href='http://10.1.75.87:8080/student/confirmRead?id="+student.getId()+"'>点击</a>",true);
            helper.setText("您的验证码是:"+code);
//            helper.setFrom("1422608105@qq.com");
            helper.setTo(email);

            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }

//    文件
//    @Test
//    public void test(){
//
//        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
//        try {
//            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
//            //标题
//            helper.setSubject("通知");
//            //内容
//            helper.setText("你好");
//            //发件人
//            helper.setFrom("40109438@qq.com");
//            //收件人
//            helper.setTo("40109438@qq.com");
//            //文件
//            helper.addAttachment("李雪仁.txt",new File("E:\\桌面\\a.txt"));
//
//            javaMailSender.send(mimeMessage);
//        } catch (MessagingException e) {
//            e.printStackTrace();
//        }
//    }


//	  图片
//    @Test
//    public void test1(){
//
//        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
//        try {
//            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
//            //标题
//            helper.setSubject("通知");
//            //内容
//            helper.setText("你好,给你看涨图片<br>" + "<img src='cid:opp'>",true);
//            //发件人
//            helper.setFrom("40109438@qq.com");
//            //收件人
//            helper.setTo("40109438@qq.com");
//            //图片
//            helper.addInline("opp",new FileSystemResource(new File("E:\\桌面\\11.gif")));
//
//            javaMailSender.send(mimeMessage);
//        } catch (MessagingException e) {
//            e.printStackTrace();
//        }
//    }
}
